<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/9/28
 * Time: 17:07
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace App\Http\model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Dotenv\Exception;
use App\Http\model\CheckRefundStatus;
use Illuminate\Support\Facades\Log;

class GetRefund extends Model
{
    /**
     * 退款退货单下载
     * @param $request
     * @param $PageIndex
     * @param $PageSize
     * @return mixed
     */
    public function GetRefundModel($result, $PageIndex, $PageSize)
    {
//        $data = DB::table('orderdetail')
//            ->select(
//                'orderdetail.OrderTime',
//                'orderdetail.OrderCode',
//                'orderdetail.exprestate',
//                'orderdetail.exprestatus',
//                'orderdetail.OIcode',
//                'orderdetail.expressname',
//                'orderdetail.expressnum',
//                'orderdetail.SKU_ID',
//                'orderdetail.ProductName',
//                'orderdetail.ProductCount',//ProductNum  int 必填 商品购买数量
//                'orderdetail.payprice',//支付金额
//                'orderdetail.allpayprice',//订单总金额  应付金额
//                'orderdetail.orStatus',
//                'orderdetail.Status',
//                'users.NickName'
//            )->whereIn('orderdetail.Status', [50, 51, 52, 59, 60, 61, 62, 69, 70, 71, 72, 79])
//            ->whereBetween('orderdetail.OrderTime', [$result['BeginTime'], $result['EndTime']])
//            ->leftJoin('users', 'orderdetail.BuyerID', '=', 'users.ID')
//            ->orderByDesc('orderdetail.OrderTime');

        $handle = DB::table('orderdetail')->select('OrderTime',
            'OrderCode',
            'exprestate',
            'exprestatus',
            'OIcode',
            'expressname',
            'expressnum',
            'SKU_ID',
            'ProductName',
            'ProductCount',//ProductNum  int 必填 商品购买数量
            'payprice',//支付金额
            'allpayprice',//订单总金额  应付金额
            'orStatus',
            'Status',
            'BuyerID'
        );
        $handle = $handle->whereIn('Status', [50, 51, 52, 59, 60, 61, 62, 69, 70, 71, 72, 79]);
        $handle = $handle->whereBetween('OrderTime', [$result['BeginTime'], $result['EndTime']]);
        $handle = $handle->orderByDesc('OrderTime');
        $totalcount = $handle->count();
        $res = $handle->offset($PageIndex)->limit($PageSize)->get();
//        dd($res);
        $array['TotalCount'] = $totalcount;
        foreach ($res as $item) {
            $item = json_decode(json_encode($item), true);
            if (empty($item['OIcode'])) {
                unset($item['OIcode']);
            }
            $orders['refundno'] = $item['OIcode'];
            $orders['platorderno'] = $item['OIcode'];
            $orders['totalamount'] = $item['allpayprice'];
            $orders['payamount'] = $item['allpayprice'];
//            dd($item);
            //  ->leftJoin('users', 'orderdetail.BuyerID', '=', 'users.ID')
            $NickName = DB::table('users')->where('ID', $item['BuyerID'])->first(['NickName']);
//            dd($NickName);
            $orders['buyernick'] = $NickName->NickName;
            $orders['sellernick'] = '北京美家臻选';

            $Content = DB::table('applyforcancelorder')->where('OrderNum', $item['OrderCode'])->first();
            $Content = json_decode(json_encode($Content), true);
            $orders['createtime'] = $Content['CreateTime'];
            $orders['updatetime'] = $Content['CreateTime'];
//            dd($item);

            //退款状态
            if ($item['Status'] == 50) {
                $orders['refundstatus'] = 'JH_01';//买家已经申请退款等待卖家同意=JH_01
            }
            if ($item['Status'] == 59 || $item['Status'] == 79) {
                $orders['refundstatus'] = 'JH_06';//退款成功
            }
            if ($item['Status'] == 52) {
                $orders['refundstatus'] = 'JH_04';//拒绝退款
            }

            //订单状态Status==0 拼团订单  99正常订单
            if ($item['Status'] == 0 && $item['orStatus'] == 0) {//订单状态Status==0 拼团订单
                $orders['orderstatus'] = 'JH_01';//代付款
            }
            if ($item['Status'] == 0 && $item['orStatus'] == 1) {
                $orders['orderstatus'] = 'JH_02';//待发货
            }
            if ($item['exprestate'] == 1) {
                $orders['orderstatus'] = 'JH_03';//待收货
            }
            if ($item['Status'] == 0 && $item['orStatus'] == 9) {
                $orders['orderstatus'] = 'JH_04';//9确认收货==已完成
            }
            if ($item['Status'] == 0 && $item['orStatus'] == 6) {
                $orders['orderstatus'] = 'JH_05';//交易关闭
            }
            if ($item['Status'] == 99 && $item['orStatus'] == 0) {//订单状态
                $orders['orderstatus'] = 'JH_01';//代付款
            }
            if ($item['Status'] == 99 && $item['orStatus'] == 1) {
                $orders['orderstatus'] = 'JH_02';//待发货
            }
            if ($item['Status'] == 99 && $item['exprestate'] == 1) {
                $orders['orderstatus'] = 'JH_03';//待收货
            }
            if ($item['Status'] == 99 && $item['orStatus'] == 9) {
                $orders['orderstatus'] = 'JH_04';//9确认收货==已完成
            }
            if ($item['Status'] == 99 && $item['orStatus'] == 6) {
                $orders['orderstatus'] = 'JH_05';//交易关闭
            }
            if ($item['Status'] == 50) {
                $orders['orderstatus'] = 'JH_11';//买家已经申请退款等待卖家同意=JH_11
            } elseif ($item['Status'] == 59 || $item['Status'] == 79) {
                $orders['orderstatus'] = 'JH_11';//退款成功
            } elseif ($item['Status'] == 52 || $item['Status'] == 72) {
                $orders['orderstatus'] = 'JH_11';//拒绝退款
            } else {
                $orders['orderstatus'] = 'JH_98';//其他
            }
//dd($item);


//            dd($item['detailtatus']);
//商品状态(其他=JH_98，买家未收到货=JH_01，买家已收到货=JH_02，买家已退货=JH_03)
            $orders['hasgoodsreturn'] = $item['exprestate'] == 1 ? true : false;

            $orders['reason'] = $Content['Content'];

            $orders['productnum'] = $item['ProductCount'];
            $orders['logisticname'] = $item['expressname'];
            $orders['logisticno'] = $item['expressnum'];

            $skuData = DB::table('sku_attrv')
                ->where('skuid', $item['SKU_ID'])
                ->join('attrvalues', 'sku_attrv.attrvalueid', '=', 'attrvalues.id')
                ->first();
            $sku = json_decode(json_encode($skuData), true);

//            dd($item);
            $ordata['RefundGoods']['PlatProductId'] = $item['SKU_ID'];
            $ordata['RefundGoods']['OuterID'] = $item['SKU_ID'];
            $ordata['RefundGoods']['Sku'] = $sku['Value'];
            $ordata['RefundGoods']['ProductName'] = $item['ProductName'];
            $ordata['RefundGoods']['RefundAmount'] = $item['payprice'];
            $ordata['RefundGoods']['Reason'] = $Content['Content'];
            $ordata['RefundGoods']['ProductNum'] = $item['ProductCount'];
            $orders['RefundGoods'] = [$ordata['RefundGoods']];

            $resData[] = $orders;

        }
//Log::info('$resData=='.json_encode($resData));

        $array['Refunds'] = isset($resData) ? $resData : '';
        $array['IsSuccess'] = true;
        $array['Message'] = "SUCCESS";
        $array['Code'] = 10000;
        return $array;
    }

    //订单状态(等待买家付款=JH_01，等待卖家发货=JH_02，等待买家确认收货=JH_03，交易成功=JH_04，交易关闭=JH_05，已暂停=JH_06，已锁定=JH_07，卖家部分发货=JH_08，订单冻结=JH_09，缺货订单=JH_10，退货订单=JH_11，投诉订单=JH_12，已拆单=JH_13，退换货订单=JH_14，其他=JH_98，所有订单=JH_99)


}